<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>PythonGUI | lilei</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/images/icons/logo.png">
    <link rel="apple-touch-icon" href="/images/icons/logo.png">
    <link rel="manifest" href="/manifest.json">
    <script type="text/javascript" src="/assets/js/load.js"></script>
    <meta name="description" content="个人网站">
    <meta name="twitter:title" content="PythonGUI">
    <meta name="twitter:description" content="">
    <meta name="twitter:card" content="summary">
    <meta name="twitter:url" content="http://www.lileiblog.xyz/Python/PythonGUI.html">
    <meta property="og:type" content="article">
    <meta property="og:title" content="PythonGUI">
    <meta property="og:description" content="">
    <meta property="og:url" content="http://www.lileiblog.xyz/Python/PythonGUI.html">
    <meta property="og:site_name" content="lilei">
    <meta itemprop="name" content="PythonGUI">
    <meta itemprop="description" content="">
    <meta name="theme-color" content="#3eaf7c">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="msapplication-TileImage" content="/images/icons/logo.png">
    <meta name="msapplication-TileColor" content="#ffffff">
    <meta name="keywords" content="lilei,vue,vuepress,vuepress-theme,theme,主题,vuepress主题,blog
        ,vuepress-blog,java,python,shell,sql,golang,script,shell script,nginx,windows,去广告,androd,google drive
        ,one drive,idea,eclipse,git,小程序">
    <meta name="referrer" content="never">
    
    <link rel="preload" href="/assets/css/0.styles.8932179b.css" as="style"><link rel="preload" href="/assets/js/app.269009d6.js" as="script"><link rel="preload" href="/assets/js/3.974b9720.js" as="script"><link rel="preload" href="/assets/js/43.ca8f222e.js" as="script"><link rel="prefetch" href="/assets/js/10.5915e99b.js"><link rel="prefetch" href="/assets/js/11.ff732762.js"><link rel="prefetch" href="/assets/js/12.8bb86d91.js"><link rel="prefetch" href="/assets/js/13.18bde5b0.js"><link rel="prefetch" href="/assets/js/14.314fae34.js"><link rel="prefetch" href="/assets/js/15.0a4ff48f.js"><link rel="prefetch" href="/assets/js/16.ffaf1503.js"><link rel="prefetch" href="/assets/js/17.49a31a6e.js"><link rel="prefetch" href="/assets/js/18.7e6443bf.js"><link rel="prefetch" href="/assets/js/19.6880cc9a.js"><link rel="prefetch" href="/assets/js/20.1fb3f255.js"><link rel="prefetch" href="/assets/js/21.0cd39eea.js"><link rel="prefetch" href="/assets/js/22.392d85cb.js"><link rel="prefetch" href="/assets/js/23.f50adb15.js"><link rel="prefetch" href="/assets/js/24.43e324e1.js"><link rel="prefetch" href="/assets/js/25.e3663205.js"><link rel="prefetch" href="/assets/js/26.da141fb8.js"><link rel="prefetch" href="/assets/js/27.6c05235a.js"><link rel="prefetch" href="/assets/js/28.c124245a.js"><link rel="prefetch" href="/assets/js/29.d185870d.js"><link rel="prefetch" href="/assets/js/30.8587d422.js"><link rel="prefetch" href="/assets/js/31.dc382b56.js"><link rel="prefetch" href="/assets/js/32.20ba32e9.js"><link rel="prefetch" href="/assets/js/33.6ecd7de9.js"><link rel="prefetch" href="/assets/js/34.2b91c13a.js"><link rel="prefetch" href="/assets/js/35.66ed0733.js"><link rel="prefetch" href="/assets/js/36.ce7bca84.js"><link rel="prefetch" href="/assets/js/37.d00bb5ac.js"><link rel="prefetch" href="/assets/js/38.c3ecbb6f.js"><link rel="prefetch" href="/assets/js/39.308f4175.js"><link rel="prefetch" href="/assets/js/4.520f6ecc.js"><link rel="prefetch" href="/assets/js/40.f4b98bc2.js"><link rel="prefetch" href="/assets/js/41.95fae64f.js"><link rel="prefetch" href="/assets/js/42.fe4f877b.js"><link rel="prefetch" href="/assets/js/44.4d4ee79e.js"><link rel="prefetch" href="/assets/js/45.79178b33.js"><link rel="prefetch" href="/assets/js/46.c3817d0b.js"><link rel="prefetch" href="/assets/js/47.b8252443.js"><link rel="prefetch" href="/assets/js/48.df9d5d3b.js"><link rel="prefetch" href="/assets/js/49.d812fdb2.js"><link rel="prefetch" href="/assets/js/5.f1a24a16.js"><link rel="prefetch" href="/assets/js/50.55cfcaa8.js"><link rel="prefetch" href="/assets/js/51.b8608b02.js"><link rel="prefetch" href="/assets/js/52.52c5e00d.js"><link rel="prefetch" href="/assets/js/53.3a9bd5fb.js"><link rel="prefetch" href="/assets/js/54.a4240c13.js"><link rel="prefetch" href="/assets/js/55.01c4b876.js"><link rel="prefetch" href="/assets/js/56.f29549a3.js"><link rel="prefetch" href="/assets/js/57.01438a04.js"><link rel="prefetch" href="/assets/js/58.29bb4626.js"><link rel="prefetch" href="/assets/js/59.e8b89408.js"><link rel="prefetch" href="/assets/js/6.8c478b39.js"><link rel="prefetch" href="/assets/js/60.bedb5381.js"><link rel="prefetch" href="/assets/js/61.bdfbe584.js"><link rel="prefetch" href="/assets/js/62.cb825d1a.js"><link rel="prefetch" href="/assets/js/63.a9516572.js"><link rel="prefetch" href="/assets/js/64.aa854ed7.js"><link rel="prefetch" href="/assets/js/65.285f883d.js"><link rel="prefetch" href="/assets/js/66.12849f37.js"><link rel="prefetch" href="/assets/js/67.c18b00e4.js"><link rel="prefetch" href="/assets/js/68.fa59daa9.js"><link rel="prefetch" href="/assets/js/69.e9d5dda4.js"><link rel="prefetch" href="/assets/js/7.cfb0d0ea.js"><link rel="prefetch" href="/assets/js/70.db126cce.js"><link rel="prefetch" href="/assets/js/71.6acb04cd.js"><link rel="prefetch" href="/assets/js/72.7ff0ab11.js"><link rel="prefetch" href="/assets/js/73.9a66da79.js"><link rel="prefetch" href="/assets/js/74.0a38a030.js"><link rel="prefetch" href="/assets/js/75.7b32dbb3.js"><link rel="prefetch" href="/assets/js/8.cb9c08e5.js"><link rel="prefetch" href="/assets/js/9.ad652cfe.js"><link rel="prefetch" href="/assets/js/vendors~flowchart.110a1065.js">
    <link rel="stylesheet" href="/assets/css/0.styles.8932179b.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/images/icons/logo.png" alt="lilei" class="logo"> <span class="site-name can-hide">lilei</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/Form/" class="nav-link">
  Form
</a></div><div class="nav-item"><a href="/IDE/" class="nav-link">
  IDE
</a></div><div class="nav-item"><a href="/Java/" class="nav-link">
  Java
</a></div><div class="nav-item"><a href="/Other/" class="nav-link">
  Other
</a></div><div class="nav-item"><a href="/Python/" class="nav-link router-link-active">
  Python
</a></div><div class="nav-item"><a href="/SQL/" class="nav-link">
  SQL
</a></div><div class="nav-item"><a href="/Shell/" class="nav-link">
  Shell
</a></div><div class="nav-item"><a href="/System/" class="nav-link">
  System
</a></div> <a href="https://github.com/woytu/notes-vuepress" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/Form/" class="nav-link">
  Form
</a></div><div class="nav-item"><a href="/IDE/" class="nav-link">
  IDE
</a></div><div class="nav-item"><a href="/Java/" class="nav-link">
  Java
</a></div><div class="nav-item"><a href="/Other/" class="nav-link">
  Other
</a></div><div class="nav-item"><a href="/Python/" class="nav-link router-link-active">
  Python
</a></div><div class="nav-item"><a href="/SQL/" class="nav-link">
  SQL
</a></div><div class="nav-item"><a href="/Shell/" class="nav-link">
  Shell
</a></div><div class="nav-item"><a href="/System/" class="nav-link">
  System
</a></div> <a href="https://github.com/woytu/notes-vuepress" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>Python</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/Python/" aria-current="page" class="sidebar-link">Python</a></li><li><a href="/Python/PythonGUI.html" aria-current="page" class="active sidebar-link">PythonGUI</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/Python/PythonGUI.html#flag" class="sidebar-link">Flag</a></li><li class="sidebar-sub-header"><a href="/Python/PythonGUI.html#wxpython" class="sidebar-link">wxPython</a></li><li class="sidebar-sub-header"><a href="/Python/PythonGUI.html#pyside" class="sidebar-link">PySide</a></li><li class="sidebar-sub-header"><a href="/Python/PythonGUI.html#打包" class="sidebar-link">打包</a></li></ul></li><li><a href="/Python/Python爬虫.html" class="sidebar-link">Python爬虫</a></li><li><a href="/Python/Python笔记.html" class="sidebar-link">Python笔记</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="pythongui"><a href="#pythongui" class="header-anchor">#</a> PythonGUI</h1> <p></p><div class="table-of-contents"><ul><li><a href="#flag">Flag</a></li><li><a href="#wxpython">wxPython</a><ul><li><a href="#方法">方法</a></li><li><a href="#wx-event的子类">wx.Event的子类</a></li><li><a href="#style样式常量">style样式常量</a></li><li><a href="#欢迎界面">欢迎界面</a></li><li><a href="#多线程防假死与线程间传递消息">多线程防假死与线程间传递消息</a></li></ul></li><li><a href="#pyside">PySide</a><ul><li><a href="#多线程防假死动态刷新界面">多线程防假死动态刷新界面</a></li></ul></li><li><a href="#打包">打包</a><ul><li><a href="#pyinstalle">PyInstalle</a></li><li><a href="#py2exe">py2exe</a></li></ul></li></ul></div><p></p> <h2 id="flag"><a href="#flag" class="header-anchor">#</a> Flag</h2> <ul><li><a href="https://github.com/pyqtgraph/pyqtgraph" target="_blank" rel="noopener noreferrer">https://github.com/pyqtgraph/pyqtgraph<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li>流程图 <a href="https://github.com/leon-thomm/Ryven" target="_blank" rel="noopener noreferrer">https://github.com/leon-thomm/Ryven<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li>自动操作GUI <a href="https://github.com/asweigart/pyautogui" target="_blank" rel="noopener noreferrer">https://github.com/asweigart/pyautogui<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> <ul><li><a href="https://silaoa.github.io/2020/2020-11-27-Python%E8%87%AA%E5%8A%A8%E6%93%8D%E4%BD%9CGUI%E7%A5%9E%E5%99%A8PyAutoGUI.html" target="_blank" rel="noopener noreferrer">Python自动操作GUI神器PyAutoGUI<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></li> <li><a href="https://github.com/pywinauto/pywinauto" target="_blank" rel="noopener noreferrer">https://github.com/pywinauto/pywinauto<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <ul><li><a href="https://github.com/zwjjiaozhu/gitchat_download" target="_blank" rel="noopener noreferrer">https://github.com/zwjjiaozhu/gitchat_download<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <p><strong>Tkinter</strong></p> <ul><li><a href="https://docs.python.org/zh-cn/3/library/tkinter.html" target="_blank" rel="noopener noreferrer">https://docs.python.org/zh-cn/3/library/tkinter.html<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <ul><li><a href="https://sourceforge.net/projects/tcl" target="_blank" rel="noopener noreferrer">https://sourceforge.net/projects/tcl<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://github.com/doudoudzj/tkinter-learn" target="_blank" rel="noopener noreferrer">https://github.com/doudoudzj/tkinter-learn<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://github.com/doudoudzj/tkinter-examples" target="_blank" rel="noopener noreferrer">https://github.com/doudoudzj/tkinter-examples<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://github.com/ParthJadhav/Tkinter-Designer" target="_blank" rel="noopener noreferrer">https://github.com/ParthJadhav/Tkinter-Designer<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <p><strong>PyQt</strong></p> <ul><li><a href="https://github.com/qt/qt5" target="_blank" rel="noopener noreferrer">https://github.com/qt/qt5<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://github.com/pyqt" target="_blank" rel="noopener noreferrer">https://github.com/pyqt<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://github.com/PyQt5/PyQt" target="_blank" rel="noopener noreferrer">https://github.com/PyQt5/PyQt<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://github.com/PyQt5/QtDesigner" target="_blank" rel="noopener noreferrer">https://github.com/PyQt5/QtDesigner<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <ul><li><a href="https://github.com/ColinDuquesnoy/QDarkStyleSheet" target="_blank" rel="noopener noreferrer">https://github.com/ColinDuquesnoy/QDarkStyleSheet<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://github.com/mottosso/Qt.py" target="_blank" rel="noopener noreferrer">https://github.com/mottosso/Qt.py<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <ul><li>PyQt5快速开发与实战 <a href="https://github.com/cxinping/PyQt5" target="_blank" rel="noopener noreferrer">https://github.com/cxinping/PyQt5<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li>PyQt5中文教程 <a href="https://github.com/maicss/PyQt5-Chinese-tutorial" target="_blank" rel="noopener noreferrer">https://github.com/maicss/PyQt5-Chinese-tutorial<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <ul><li><a href="https://blog.csdn.net/wowocpp/article/details/105759105" target="_blank" rel="noopener noreferrer">pyqt5 参考代码 github 参考项目<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <p><strong>PyGObject</strong></p> <ul><li><a href="https://pygobject.readthedocs.io/en/latest" target="_blank" rel="noopener noreferrer">https://pygobject.readthedocs.io/en/latest/<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <blockquote><p><code>PyGObject</code>是一个<code>Python</code>包，它为基于<code>GObject</code>的库提供绑定，例如<code>GTK</code>、<code>GStreamer</code>、<code>WebKitGTK</code>、<code>GLib</code>、<code>GIO</code>等等。</p></blockquote> <blockquote><p>它支持<code>Linux</code>，<code>Windows</code>和<code>macOS</code>，适用于<code>Python2.7+</code>、<code>Python3.5+</code>、<code>PyPy</code>和<code>PyPy3</code>。</p></blockquote> <h2 id="wxpython"><a href="#wxpython" class="header-anchor">#</a> wxPython</h2> <ul><li><a href="https://github.com/wxWidgets/wxWidgets" target="_blank" rel="noopener noreferrer">https://github.com/wxWidgets/wxWidgets<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://wxpython.org/pages/overview" target="_blank" rel="noopener noreferrer">https://wxpython.org/pages/overview/<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <blockquote><p><code>wxPython</code>是一个用于<code>Python</code>编程语言的跨平台<code>GUI</code>工具包。包含流行的<code>wxWidgets</code>跨平台库的<code>GUI</code>组件，该库是用<code>C++</code>编写的。</p></blockquote> <blockquote><p>目前支持的平台是<code>Windows</code>、<code>Mac OS X</code>、<code>macOS</code>、<code>Linux</code>或其他具有<code>GTK2</code>或<code>GTK3</code>库的类<code>Unix</code>系统。</p></blockquote> <ul><li><a href="http://thisis.yorven.site/blog/index.php/2017/09/16/wxpython-jiaocheng-yi" target="_blank" rel="noopener noreferrer">http://thisis.yorven.site/blog/index.php/2017/09/16/wxpython-jiaocheng-yi/<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://github.com/necan/wxPython-tutorial/blob/master/1.%E4%BB%8B%E7%BB%8D.md" target="_blank" rel="noopener noreferrer">https://github.com/necan/wxPython-tutorial/blob/master/1.%E4%BB%8B%E7%BB%8D.md<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://jusene.github.io/2017/12/26/wxpython" target="_blank" rel="noopener noreferrer">https://jusene.github.io/2017/12/26/wxpython/<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://github.com/ubbn/wxPython" target="_blank" rel="noopener noreferrer">https://github.com/ubbn/wxPython<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <blockquote><p><code>wx.Window</code> 是一个基类，许多构件从它继承。包括<code>wx.Frame</code>构件。可以在所有的子类中使用<code>wx.Window</code>的方法。</p> <p><code>wx.adv</code>模块包含比核心命名空间中更高级和/或更少使用的类。
它们在单独的模块中提供，以帮助减少那些不需要任何这些类的应用程序的开销和依赖性。</p></blockquote> <h3 id="方法"><a href="#方法" class="header-anchor">#</a> 方法</h3> <ul><li><code>SetTitle( string title )</code> 设置窗口标题。只可用于框架和对话框</li> <li><code>SetToolTip( wx.ToolTip tip )</code> 为窗口添加提示</li> <li><code>SetSize( wx.Size size )</code> 设置窗口的尺寸</li> <li><code>SetPosition( wx.Point pos )</code> 设置窗口出现的位置</li> <li><code>Show( show = True )</code> 显示或隐藏窗口。其中的参数可以为<code>True</code>或<code>False</code></li> <li><code>Move( wx.Point pos )</code> 将窗口移动到指定位置</li> <li><code>SetCursor( wx.StockCursor id )</code> 设置窗口的鼠标指针样式</li> <li><code>Refresh</code> 刷新窗口</li> <li><code>wx.CallLater(millis, callableObj, *args, **kwargs)</code> 最抽象的线程安全函数</li> <li><code>wx.CallAfter(callable, *args, **kwargs)</code> 该函数是在当前和待处理事件处理程序完成后调用指定的函数。</li></ul> <blockquote><p>这个方法是利用PostEvent()来实现的。执行这个方法后，将在主事件循环中加入一个事件，然后通过事件循环进行处理。
这其实是一种异步的方法，适用于一个非GUI的处理过程要调用GUI的方法，或子线程调用主线程的方法，
或在一个事件处理函数中异步调用另一个事件处理，还有就是上面的问题。这个方法使用很方便，不需要自定义事件，绑定事件，Post事件。</p></blockquote> <blockquote><p>示例：<code>wx.CallAfter(pub.sendMessage, &quot;update&quot;, msg=text)</code> 订阅update主题并发送消息</p></blockquote> <ul><li><code>wx.FutureCall(milliseconds, callable, *args, **kwargs)</code> 从wx.Timer中派生出来的，它的作用是在指定时间之后执行一个方法。</li> <li><code>pub.subscribe(self.update_display, &quot;update&quot;)</code> 发布一个叫update的主题，触发update_display函数</li> <li><code>pub.sendMessage(&quot;update&quot;, msg='测试', status=0)</code>给update主题发送消息，<code>msg</code>和<code>status</code>都是自定义键值对</li></ul> <h3 id="wx-event的子类"><a href="#wx-event的子类" class="header-anchor">#</a> wx.Event的子类</h3> <ul><li><code>wx.CloseEvent</code>:框架关闭时触发，事件类型有普通框架关闭和系统关闭事件。</li> <li><code>wx.CommandEvent</code>：与窗口部件的简单的交互都会触发此事件，如按钮单击，菜单项选择等。</li> <li><code>wx.KeyEvent</code>:按键动作。</li> <li><code>wx.MouseEvent</code>：鼠标事件。</li> <li><code>wx.PaintEvent</code>:当窗口内容被重画时触发。</li> <li><code>wx.SizeEvent</code>:窗口大小或布局改变时触发。</li> <li><code>wx.TimerEvent</code>:由类wx.Timer创建，定期事件。</li></ul> <h3 id="style样式常量"><a href="#style样式常量" class="header-anchor">#</a> style样式常量</h3> <ul><li>Frame</li></ul> <table><thead><tr><th>参数</th> <th>描述</th> <th>说明</th></tr></thead> <tbody><tr><td>wx.DEFAULT_FRAME_STYLE</td> <td>默认的框架样式</td> <td></td></tr> <tr><td>wx.CAPTION</td> <td>标题</td> <td></td></tr> <tr><td>wx.MINIMIZE_BOX</td> <td>缩小</td> <td></td></tr> <tr><td>wx.MAXIMIZE_BOX</td> <td>放大</td> <td></td></tr> <tr><td>wx.CLOSE_BOX</td> <td>关闭</td> <td></td></tr> <tr><td>wx.SYSTEM_MENU</td> <td>系统菜单</td> <td></td></tr> <tr><td>wx.RESIZE_BORDER</td> <td>恢复窗口大小</td> <td></td></tr> <tr><td>wx.STAY_ON_TOP</td> <td>固定不能移动</td> <td></td></tr> <tr><td>wx.FRAME_FLOAT_ON_PARENT</td> <td></td> <td></td></tr></tbody></table> <ul><li>StaticText</li></ul> <table><thead><tr><th>参数</th> <th>描述</th> <th>说明</th></tr></thead> <tbody><tr><td>wx.ALIGN_LEFT</td> <td>控制标签的大小及对齐</td> <td></td></tr> <tr><td>wx.ALIGN_RIGHT</td> <td>控制标签的大小及对齐</td> <td></td></tr> <tr><td>wx.ALIGN_CENTER</td> <td>控制标签的大小及对齐</td> <td></td></tr> <tr><td>wx.ST_NO_AUTORESIZE</td> <td>防止标签的自动调整大小</td> <td></td></tr> <tr><td>wx.ST_ELLIPSIZE_START</td> <td>省略号(...)显示在开始，如果文本的大小大于标签尺寸</td> <td></td></tr> <tr><td>wx.ST_ELLIPSIZE_MIDDLE</td> <td>省略号(...)显示在中间，如果文本的大小大于标签尺寸</td> <td></td></tr> <tr><td>wx.ST_ELLIPSIZE_END</td> <td>省略号(...)显示在结尾，如果文本的大小大于标签尺寸</td> <td></td></tr></tbody></table> <ul><li>Font</li></ul> <table><thead><tr><th>参数</th> <th>描述</th> <th>说明</th></tr></thead> <tbody><tr><td>Wx.FONTSTYLE_NORMAL</td> <td>字体绘制不使用倾斜</td> <td>fontfamiy参数</td></tr> <tr><td>wx.FONTSTYLE_ITALIC</td> <td>字体是斜体</td> <td>fontfamiy参数</td></tr> <tr><td>wx.FONTSTYLE_SLANT</td> <td>字体是倾斜的，但以罗马风格形式</td> <td>fontfamiy参数</td></tr> <tr><td>Wx.FONTWEIGHT_NORMAL</td> <td>普通字体</td> <td>fontweight参数</td></tr> <tr><td>wx.FONTWEIGHT_LIGHT</td> <td>高亮字体</td> <td>fontweight参数</td></tr> <tr><td>wx.FONTWEIGHT_BOLD</td> <td>粗体</td> <td>fontweight参数</td></tr></tbody></table> <ul><li>TextCtrl</li></ul> <table><thead><tr><th>参数</th> <th>描述</th> <th>说明</th></tr></thead> <tbody><tr><td>wx.TE_MULTILINE</td> <td>文本控件允许多行，如果未指定该样式，换行字符不应该在控件中使用</td> <td></td></tr> <tr><td>wx.TE_PASSWORD</td> <td>文本将回显星号</td> <td></td></tr> <tr><td>wx.TE_READONLY</td> <td>文本将不可编辑</td> <td></td></tr> <tr><td>wx.TE_LEFT</td> <td>在控件中的文本将左对齐</td> <td></td></tr> <tr><td>wx.TE_CENTER</td> <td>在控件中的文本将居中对齐</td> <td></td></tr> <tr><td>wx.TE_RIGHT</td> <td>在控件中的文本将居右对齐</td> <td></td></tr> <tr><td>wx.HSCROLL</td> <td>如果文字控制元件是多行的，并且如果该样式被声明了，那么长的行将不会自动换行，并显示水平滚动条。该选项在GTK+中被忽略。</td> <td></td></tr> <tr><td>wx.TE_AUTO_URL</td> <td>如果丰富文字选项被设定并且平台支援的话，那么当用户的鼠标位于文字中的一个URL上或在该URL上敲击时，这个样式将导致一个事件被生成。</td> <td></td></tr> <tr><td>wx.TE_DONTWRAP</td> <td>wx.HSCROLL的别名。</td> <td></td></tr> <tr><td>wx.TE_LINEWRAP</td> <td>对于太长的行，以字元为界换行。某些操作系统可能会忽略该样式。</td> <td></td></tr> <tr><td>wx.TE_RICH</td> <td>用于Windows下，丰富文字控制元件用作基本的视窗部件。这允许样式文字的使用。</td> <td></td></tr> <tr><td>wx.TE_RICH2</td> <td>用于Windows下，把最新版本的丰富文字控制元件用作基本的视窗部件。</td> <td></td></tr> <tr><td>wx.TE_WORDWRAP</td> <td>对于太长的行，以单词为界换行。许多操作系统会忽略该样式。</td> <td></td></tr></tbody></table> <ul><li>CheckBox</li></ul> <table><thead><tr><th>参数</th> <th>描述</th> <th>说明</th></tr></thead> <tbody><tr><td>wx.CHK_2STATE</td> <td>创建两个状态复选框，默认</td> <td></td></tr> <tr><td>wx.CHK_3STATE</td> <td>创建三态复选框</td> <td></td></tr> <tr><td>wx.ALIGN_RIGHT</td> <td>把一个盒子标签放在复选框的左侧</td> <td></td></tr></tbody></table> <ul><li>ComboBox</li></ul> <table><thead><tr><th>参数</th> <th>描述</th> <th>说明</th></tr></thead> <tbody><tr><td>wx.CB_SIMPLE</td> <td>组合框与永久显示的列表</td> <td></td></tr> <tr><td>wx.CB_DROPDOWN</td> <td>组合框与下拉列表</td> <td></td></tr> <tr><td>wx.CB_READONLY</td> <td>选择的项目是不可编辑</td> <td></td></tr> <tr><td>wx.CB_SORT</td> <td>列表显示按字母顺序</td> <td></td></tr></tbody></table> <ul><li>Guage</li></ul> <table><thead><tr><th>参数</th> <th>描述</th> <th>说明</th></tr></thead> <tbody><tr><td>wx.GA_HORIZONTAL</td> <td>进度条的横向布局</td> <td></td></tr> <tr><td>wx.GA_VERTICAL</td> <td>进度条的垂直布局</td> <td></td></tr> <tr><td>wx.GA_SMOOTH</td> <td>平滑的进度条使用一个像素宽度的更新步骤</td> <td></td></tr> <tr><td>wx.GA_TEXT</td> <td>显示当前值在百分比形式</td> <td></td></tr></tbody></table> <ul><li>Slider</li></ul> <table><thead><tr><th>参数</th> <th>描述</th> <th>说明</th></tr></thead> <tbody><tr><td>wx.SL_HORIZONTAL</td> <td>水平滑块</td> <td></td></tr> <tr><td>wx.SL_VERTICAL</td> <td>垂直滑块</td> <td></td></tr> <tr><td>wx.SL_AUTOTICKS</td> <td>在滑块显示tickmarks</td> <td></td></tr> <tr><td>wx.SL_LABELS</td> <td>显示最小值，最大值和当前值</td> <td></td></tr> <tr><td>wx.SL_MIN_MAX_LABELS</td> <td>显示最小值和最大值</td> <td></td></tr> <tr><td>wx.SL_VALUE_LABEL</td> <td>只显示当前值</td> <td></td></tr></tbody></table> <ul><li>ToolBar</li></ul> <table><thead><tr><th>参数</th> <th>描述</th> <th>说明</th></tr></thead> <tbody><tr><td>wx.TB_FLAT</td> <td>提供该工具栏平面效果</td> <td></td></tr> <tr><td>wx.TB_HORIZONTAL</td> <td>指定水平布局(默认)</td> <td></td></tr> <tr><td>wx.TB_VERTICAL</td> <td>指定垂直布局</td> <td></td></tr> <tr><td>wx.TB_DEFAULT_STYLE</td> <td>结合wx.TB_FLAT和wx.TB_HORIZONTAL</td> <td></td></tr> <tr><td>wx.TB_DOCKABLE</td> <td>使工具栏浮动和可停靠</td> <td></td></tr> <tr><td>wx.TB_NO_TOOLTIPS</td> <td>当鼠标悬停在工具栏不显示简短帮助工具提示</td> <td></td></tr> <tr><td>wx.TB_NOICONS</td> <td>指定工具栏按钮没有图标，默认显示</td> <td></td></tr> <tr><td>wx.TB_TEXT</td> <td>显示在工具栏按钮上的文本，默认只有图标显示</td> <td></td></tr></tbody></table> <ul><li>Dialog</li></ul> <table><thead><tr><th>参数</th> <th>描述</th> <th>说明</th></tr></thead> <tbody><tr><td>wx.CAPTION</td> <td>对话框的文字说明</td> <td></td></tr> <tr><td>wx.DEFAULT_DIALOG_STYLE</td> <td>wx.CLOSE_BOX和wx.SYSTEM_MENU的组合</td> <td></td></tr> <tr><td>wx.RESIZE_BORDER</td> <td>显示可调整窗口的大小</td> <td></td></tr> <tr><td>wx.SYSTEM_MENU</td> <td>显示系统菜单</td> <td></td></tr> <tr><td>wx.CLOSE_BOX</td> <td>框架上显示关闭</td> <td></td></tr> <tr><td>wx.MAXIMIZE_BOX</td> <td>在对话框中显示一个最大化框</td> <td></td></tr> <tr><td>wx.MINIMIZE_BOX</td> <td>在对话框中显示一个最小化框</td> <td></td></tr> <tr><td>wx.STAY_ON_TOP</td> <td>确保对话框停留在所有其他窗口的顶部</td> <td></td></tr> <tr><td>wx.DIALOG_NO_PARENT</td> <td>为防止产生无看管对话框，不推荐使用模态对话框</td> <td></td></tr></tbody></table> <ul><li>FileDialog</li></ul> <table><thead><tr><th>参数</th> <th>描述</th> <th>说明</th></tr></thead> <tbody><tr><td>wx.FD_DEFAULT_STYLE</td> <td>相当于wx.FD_OPEN</td> <td></td></tr> <tr><td>wx.FD_OPEN</td> <td>这是一个打开的对话框，该对话框的默认按钮的标签是‘打开’</td> <td></td></tr> <tr><td>wx.FD_SAVE</td> <td>这是一个保存对话框，该对话框的默认按钮的标签是‘保存’</td> <td></td></tr> <tr><td>wx.FD_OVERWRITE_PROMPT</td> <td>对于只保存的对话框，如果一个文件将被覆盖，提示进行确认</td> <td></td></tr> <tr><td>wx.FD_MULTIPLE</td> <td>仅适合打开的对话框，允许选择多个文件</td> <td></td></tr> <tr><td>wx.FD_CHANGE_DIR</td> <td>更改当前工作目录到用户选择的文件目录</td> <td></td></tr></tbody></table> <ul><li>SplitterWindow</li></ul> <table><thead><tr><th>参数</th> <th>描述</th> <th>说明</th></tr></thead> <tbody><tr><td>wx.SP_3D</td> <td>绘制3d效果边框和窗扇</td> <td></td></tr> <tr><td>wx.SP_THIN_SASH</td> <td>绘制一个薄窗扇</td> <td></td></tr> <tr><td>wx.SP_3DSASH</td> <td>绘制3d效果窗扇</td> <td></td></tr> <tr><td>wx.SP_BORDER</td> <td>绘制标准边框</td> <td></td></tr> <tr><td>wx.SP_NOBORDER</td> <td>无边框</td> <td></td></tr> <tr><td>wx.SP_PERMIT_UNSPLIT</td> <td>总是允许取消分割，即使采用最小的窗格大小不为零</td> <td></td></tr></tbody></table> <ul><li>HTMLWindow</li></ul> <table><thead><tr><th>参数</th> <th>描述</th> <th>说明</th></tr></thead> <tbody><tr><td>wx.HW_SCROLLBAR_NEVER</td> <td>永远不显示滚动条，即使是页面比窗口大</td> <td></td></tr> <tr><td>wx.HW_SCROLLBAR_AUTO</td> <td>只在页面大小超过窗口大小显示滚动条</td> <td></td></tr> <tr><td>wx.HW_NO_SELECTION</td> <td>不要让用户选择文本</td> <td></td></tr></tbody></table> <h3 id="欢迎界面"><a href="#欢迎界面" class="header-anchor">#</a> 欢迎界面</h3> <div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">import</span> wx<span class="token punctuation">.</span>adv
<span class="token keyword">import</span> time
<span class="token keyword">def</span> <span class="token function">creat_splash</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment"># create a welcome screen</span>
    screen <span class="token operator">=</span> wx<span class="token punctuation">.</span>Image<span class="token punctuation">(</span><span class="token string">&quot;file.png&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span>ConvertToBitmap<span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token comment"># 参数1000代表 界面显示1s，后面添加1s延时，等欢迎界面结束后再启动主界面。</span>
    wx<span class="token punctuation">.</span>adv<span class="token punctuation">.</span>SplashScreen<span class="token punctuation">(</span>screen<span class="token punctuation">,</span> wx<span class="token punctuation">.</span>adv<span class="token punctuation">.</span>SPLASH_CENTRE_ON_SCREEN <span class="token operator">|</span> wx<span class="token punctuation">.</span>adv<span class="token punctuation">.</span>SPLASH_TIMEOUT<span class="token punctuation">,</span> <span class="token number">1000</span><span class="token punctuation">,</span> <span class="token boolean">None</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>
    time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
</code></pre></div><div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">import</span> wx
<span class="token comment">#自定义一个窗口类MyFrame</span>
<span class="token keyword">class</span> <span class="token class-name">MyFrame</span><span class="token punctuation">(</span>wx<span class="token punctuation">.</span>Frame<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token builtin">super</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>__init__<span class="token punctuation">(</span>parent<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">,</span>title<span class="token operator">=</span><span class="token string">&quot;下拉列表&quot;</span><span class="token punctuation">,</span>size<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">400</span><span class="token punctuation">,</span><span class="token number">130</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>Center<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">#设置窗口居中</span>
        <span class="token comment">#放一个面板，用于布局其他控件</span>
        panel<span class="token operator">=</span>wx<span class="token punctuation">.</span>Panel<span class="token punctuation">(</span>parent<span class="token operator">=</span>self<span class="token punctuation">)</span>
        hbox1<span class="token operator">=</span>wx<span class="token punctuation">.</span>BoxSizer<span class="token punctuation">(</span>wx<span class="token punctuation">.</span>HORIZONTAL<span class="token punctuation">)</span>
        <span class="token comment">#创建静态文本</span>
        statictext<span class="token operator">=</span>wx<span class="token punctuation">.</span>StaticText<span class="token punctuation">(</span>panel<span class="token punctuation">,</span>label<span class="token operator">=</span><span class="token string">'选择你喜欢的编程语言：'</span><span class="token punctuation">)</span>
        list1<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">'Python'</span><span class="token punctuation">,</span><span class="token string">'Java'</span><span class="token punctuation">,</span><span class="token string">&quot;C++&quot;</span><span class="token punctuation">]</span>
        ch1<span class="token operator">=</span>wx<span class="token punctuation">.</span>ComboBox<span class="token punctuation">(</span>panel<span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span>value<span class="token operator">=</span><span class="token string">'C'</span><span class="token punctuation">,</span>choices<span class="token operator">=</span>list1<span class="token punctuation">,</span>style<span class="token operator">=</span>wx<span class="token punctuation">.</span>CB_SORT<span class="token punctuation">)</span>
        <span class="token comment">#添加事件处理</span>
        self<span class="token punctuation">.</span>Bind<span class="token punctuation">(</span>wx<span class="token punctuation">.</span>EVT_COMBOBOX<span class="token punctuation">,</span>self<span class="token punctuation">.</span>on_combobox<span class="token punctuation">,</span>ch1<span class="token punctuation">)</span>

        hbox1<span class="token punctuation">.</span>Add<span class="token punctuation">(</span>statictext<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span>flag<span class="token operator">=</span>wx<span class="token punctuation">.</span>LEFT <span class="token operator">|</span>wx<span class="token punctuation">.</span>RIGHT<span class="token operator">|</span>wx<span class="token punctuation">.</span>FIXED_MINSIZE<span class="token punctuation">,</span>border<span class="token operator">=</span><span class="token number">5</span><span class="token punctuation">)</span>
        hbox1<span class="token punctuation">.</span>Add<span class="token punctuation">(</span>ch1<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span>flag<span class="token operator">=</span>wx<span class="token punctuation">.</span>LEFT <span class="token operator">|</span>wx<span class="token punctuation">.</span>RIGHT<span class="token operator">|</span>wx<span class="token punctuation">.</span>FIXED_MINSIZE<span class="token punctuation">,</span>border<span class="token operator">=</span><span class="token number">5</span><span class="token punctuation">)</span>

        hbox2<span class="token operator">=</span>wx<span class="token punctuation">.</span>BoxSizer<span class="token punctuation">(</span>wx<span class="token punctuation">.</span>HORIZONTAL<span class="token punctuation">)</span>
        statictext<span class="token operator">=</span>wx<span class="token punctuation">.</span>StaticText<span class="token punctuation">(</span>panel<span class="token punctuation">,</span>label<span class="token operator">=</span><span class="token string">'选择性别：'</span><span class="token punctuation">)</span>
        list2<span class="token operator">=</span><span class="token punctuation">[</span><span class="token string">'男'</span><span class="token punctuation">,</span><span class="token string">'女'</span><span class="token punctuation">]</span>
        ch2<span class="token operator">=</span>wx<span class="token punctuation">.</span>Choice<span class="token punctuation">(</span>panel<span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span>choices<span class="token operator">=</span>list2<span class="token punctuation">)</span>
        hbox2<span class="token punctuation">.</span>Add<span class="token punctuation">(</span>statictext<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span>flag<span class="token operator">=</span>wx<span class="token punctuation">.</span>LEFT <span class="token operator">|</span>wx<span class="token punctuation">.</span>RIGHT<span class="token operator">|</span>wx<span class="token punctuation">.</span>FIXED_MINSIZE<span class="token punctuation">,</span>border<span class="token operator">=</span><span class="token number">5</span><span class="token punctuation">)</span>
        hbox2<span class="token punctuation">.</span>Add<span class="token punctuation">(</span>ch2<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span>flag<span class="token operator">=</span>wx<span class="token punctuation">.</span>LEFT <span class="token operator">|</span>wx<span class="token punctuation">.</span>RIGHT<span class="token operator">|</span>wx<span class="token punctuation">.</span>FIXED_MINSIZE<span class="token punctuation">,</span>border<span class="token operator">=</span><span class="token number">5</span><span class="token punctuation">)</span>

        <span class="token comment">#添加事件处理</span>
        self<span class="token punctuation">.</span>Bind<span class="token punctuation">(</span>wx<span class="token punctuation">.</span>EVT_CHOICE<span class="token punctuation">,</span>self<span class="token punctuation">.</span>on_choice<span class="token punctuation">,</span>ch2<span class="token punctuation">)</span>
        vbox<span class="token operator">=</span>wx<span class="token punctuation">.</span>BoxSizer<span class="token punctuation">(</span>wx<span class="token punctuation">.</span>VERTICAL<span class="token punctuation">)</span>
        vbox<span class="token punctuation">.</span>Add<span class="token punctuation">(</span>hbox1<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span>flag<span class="token operator">=</span>wx<span class="token punctuation">.</span>ALL<span class="token operator">|</span>wx<span class="token punctuation">.</span>EXPAND<span class="token punctuation">,</span>border<span class="token operator">=</span><span class="token number">5</span><span class="token punctuation">)</span>
        vbox<span class="token punctuation">.</span>Add<span class="token punctuation">(</span>hbox2<span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span>flag<span class="token operator">=</span>wx<span class="token punctuation">.</span>ALL<span class="token operator">|</span>wx<span class="token punctuation">.</span>EXPAND<span class="token punctuation">,</span>border<span class="token operator">=</span><span class="token number">5</span><span class="token punctuation">)</span>
        

        <span class="token comment">#ListBox类实例 wx.LB_SINGLE只能选择单个</span>
        self<span class="token punctuation">.</span>listbox1 <span class="token operator">=</span> wx<span class="token punctuation">.</span>ListBox<span class="token punctuation">(</span>panel<span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">,</span><span class="token number">80</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">60</span><span class="token punctuation">)</span><span class="token punctuation">,</span>list1<span class="token punctuation">,</span>wx<span class="token punctuation">.</span>LB_SINGLE<span class="token punctuation">)</span>
        <span class="token comment"># wx.LB_MULTIPLE多选</span>
        self<span class="token punctuation">.</span>listbox2 <span class="token operator">=</span> wx<span class="token punctuation">.</span>ListBox<span class="token punctuation">(</span>panel<span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">,</span> <span class="token number">150</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">60</span><span class="token punctuation">)</span><span class="token punctuation">,</span> list2<span class="token punctuation">,</span> wx<span class="token punctuation">.</span>LB_MULTIPLE<span class="token punctuation">)</span>
        <span class="token comment">#CheckListBox类实例</span>
        self<span class="token punctuation">.</span>listbox3 <span class="token operator">=</span> wx<span class="token punctuation">.</span>CheckListBox<span class="token punctuation">(</span>panel<span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">300</span><span class="token punctuation">,</span><span class="token number">80</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">60</span><span class="token punctuation">)</span><span class="token punctuation">,</span>list1<span class="token punctuation">)</span>
        <span class="token comment">#Choice类实例</span>
        self<span class="token punctuation">.</span>listbox4 <span class="token operator">=</span> wx<span class="token punctuation">.</span>Choice<span class="token punctuation">(</span>panel<span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">300</span><span class="token punctuation">,</span><span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">40</span><span class="token punctuation">)</span><span class="token punctuation">,</span>list2<span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>listbox4<span class="token punctuation">.</span>Bind<span class="token punctuation">(</span>wx<span class="token punctuation">.</span>EVT_CHOICE<span class="token punctuation">,</span>self<span class="token punctuation">.</span>One_Play<span class="token punctuation">)</span>
        <span class="token comment">#进度条展示</span>
        self<span class="token punctuation">.</span>gauge1 <span class="token operator">=</span> wx<span class="token punctuation">.</span>Gauge<span class="token punctuation">(</span>panel<span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">100</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">,</span> <span class="token number">250</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">60</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>value <span class="token operator">=</span> <span class="token number">1</span>
        self<span class="token punctuation">.</span>gauge1<span class="token punctuation">.</span>SetValue<span class="token punctuation">(</span>self<span class="token punctuation">.</span>value<span class="token punctuation">)</span>
        <span class="token comment">#将wx空闲的事件绑定到进度条上</span>
        self<span class="token punctuation">.</span>Bind<span class="token punctuation">(</span>wx<span class="token punctuation">.</span>EVT_IDLE<span class="token punctuation">,</span>self<span class="token punctuation">.</span>Gauge_Test<span class="token punctuation">)</span>
        <span class="token comment">#滑块</span>
        self<span class="token punctuation">.</span>slider <span class="token operator">=</span> wx<span class="token punctuation">.</span>Slider<span class="token punctuation">(</span>panel<span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">100</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token number">300</span><span class="token punctuation">,</span> <span class="token number">350</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">60</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>slider<span class="token punctuation">.</span>Bind<span class="token punctuation">(</span>wx<span class="token punctuation">.</span>EVT_SCROLL<span class="token punctuation">,</span>self<span class="token punctuation">.</span>Slider_Test<span class="token punctuation">)</span>

        panel<span class="token punctuation">.</span>SetSizer<span class="token punctuation">(</span>vbox<span class="token punctuation">)</span>

    <span class="token keyword">def</span> <span class="token function">on_combobox</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span>event<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;选择{0}&quot;</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>event<span class="token punctuation">.</span>GetString<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

    <span class="token keyword">def</span> <span class="token function">on_choice</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span>event<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;选择{0}&quot;</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>event<span class="token punctuation">.</span>GetString<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token comment">#自定以一个应用程序类</span>
<span class="token keyword">class</span> <span class="token class-name">App</span><span class="token punctuation">(</span>wx<span class="token punctuation">.</span>App<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">OnInit</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token comment">#创建窗口对象</span>
        frame<span class="token operator">=</span>MyFrame<span class="token punctuation">(</span><span class="token punctuation">)</span>
        frame<span class="token punctuation">.</span>Show<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span> <span class="token boolean">True</span>
    <span class="token keyword">def</span> <span class="token function">OnExit</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&quot;应用程序退出&quot;</span><span class="token punctuation">)</span>
        <span class="token keyword">return</span> <span class="token number">0</span>

<span class="token keyword">if</span> __name__<span class="token operator">==</span><span class="token string">'__main__'</span><span class="token punctuation">:</span>
    app<span class="token operator">=</span>App<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token comment">#创建自定以对象App</span>
    app<span class="token punctuation">.</span>MainLoop<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre></div><h3 id="多线程防假死与线程间传递消息"><a href="#多线程防假死与线程间传递消息" class="header-anchor">#</a> 多线程防假死与线程间传递消息</h3> <ul><li><a href="http://codingdict.com/blog/article/2019/7/11/10556.html" target="_blank" rel="noopener noreferrer">wxpython线程安全方法<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <blockquote><p><code>WxPython4.0</code>以后不用<code>wx.lib.pubsub</code>来做消息通讯了，改用<code>pubsub</code>，如果继续用<code>wx.lib.pubsub</code>，会有警告提示</p></blockquote> <div class="language-bash extra-class"><pre class="language-bash"><code>pip <span class="token function">install</span> pypubsub
</code></pre></div><ul><li>示例一</li></ul> <div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">import</span> time
<span class="token keyword">import</span> wx
<span class="token keyword">from</span> threading <span class="token keyword">import</span> Thread
<span class="token keyword">from</span> pubsub <span class="token keyword">import</span> pub


<span class="token keyword">def</span> <span class="token function">work_proc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">&quot;&quot;&quot;
    耗时长的代码
    :return:
    &quot;&quot;&quot;</span>
    sum_x <span class="token operator">=</span> <span class="token number">0</span>
    <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">101</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        sum_x <span class="token operator">=</span> sum_x <span class="token operator">+</span> i
        time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">0.1</span><span class="token punctuation">)</span>
        pub<span class="token punctuation">.</span>sendMessage<span class="token punctuation">(</span><span class="token string">&quot;update&quot;</span><span class="token punctuation">,</span> msg<span class="token operator">=</span><span class="token string">'计算{} , 合计 {}'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span> sum_x<span class="token punctuation">)</span><span class="token punctuation">,</span> status<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">)</span>
    <span class="token keyword">return</span> sum_x


<span class="token keyword">class</span> <span class="token class-name">WorkThread</span><span class="token punctuation">(</span>Thread<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token triple-quoted-string string">&quot;&quot;&quot;
    线程调用耗时长代码
    &quot;&quot;&quot;</span>

    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token triple-quoted-string string">&quot;&quot;&quot;
        线程实例化时立即启动
        &quot;&quot;&quot;</span>
        Thread<span class="token punctuation">.</span>__init__<span class="token punctuation">(</span>self<span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>start<span class="token punctuation">(</span><span class="token punctuation">)</span>

    <span class="token keyword">def</span> <span class="token function">run</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token triple-quoted-string string">&quot;&quot;&quot;
        线程执行的代码
        :return:
        &quot;&quot;&quot;</span>
        result <span class="token operator">=</span> work_proc<span class="token punctuation">(</span><span class="token punctuation">)</span>
        time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>
        pub<span class="token punctuation">.</span>sendMessage<span class="token punctuation">(</span><span class="token string">&quot;update&quot;</span><span class="token punctuation">,</span> msg<span class="token operator">=</span><span class="token string">'计算完成，结果 {}'</span><span class="token punctuation">.</span><span class="token builtin">format</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">,</span> status<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span>


<span class="token keyword">class</span> <span class="token class-name">MainForm</span><span class="token punctuation">(</span>wx<span class="token punctuation">.</span>Frame<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> parent<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        wx<span class="token punctuation">.</span>Frame<span class="token punctuation">.</span>__init__<span class="token punctuation">(</span>self<span class="token punctuation">,</span> parent<span class="token punctuation">,</span> <span class="token builtin">id</span><span class="token operator">=</span>wx<span class="token punctuation">.</span>ID_ANY<span class="token punctuation">,</span> title<span class="token operator">=</span><span class="token string">&quot;Bajins工具&quot;</span><span class="token punctuation">,</span> pos<span class="token operator">=</span>wx<span class="token punctuation">.</span>DefaultPosition<span class="token punctuation">,</span>
                          style<span class="token operator">=</span>wx<span class="token punctuation">.</span>DEFAULT_FRAME_STYLE <span class="token operator">|</span> wx<span class="token punctuation">.</span>TAB_TRAVERSAL<span class="token punctuation">)</span>
        <span class="token comment"># 允许指定最小和最大窗口大小以及窗口大小增量。</span>
        self<span class="token punctuation">.</span>SetSizeHints<span class="token punctuation">(</span>wx<span class="token punctuation">.</span>Size<span class="token punctuation">(</span><span class="token number">400</span><span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">)</span><span class="token punctuation">,</span> wx<span class="token punctuation">.</span>DefaultSize<span class="token punctuation">,</span> wx<span class="token punctuation">.</span>DefaultSize<span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>CreateStatusBar<span class="token punctuation">(</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>SetStatusText<span class="token punctuation">(</span><span class="token string">&quot;启动完成!&quot;</span><span class="token punctuation">)</span>

        sizer2 <span class="token operator">=</span> wx<span class="token punctuation">.</span>GridSizer<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>m_button2 <span class="token operator">=</span> wx<span class="token punctuation">.</span>Button<span class="token punctuation">(</span>self<span class="token punctuation">,</span> wx<span class="token punctuation">.</span>ID_ANY<span class="token punctuation">,</span> <span class="token string">&quot;开始计算&quot;</span><span class="token punctuation">,</span> wx<span class="token punctuation">.</span>DefaultPosition<span class="token punctuation">,</span> wx<span class="token punctuation">.</span>DefaultSize<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>
        sizer2<span class="token punctuation">.</span>Add<span class="token punctuation">(</span>self<span class="token punctuation">.</span>m_button2<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> wx<span class="token punctuation">.</span>ALL <span class="token operator">|</span> wx<span class="token punctuation">.</span>ALIGN_CENTER_HORIZONTAL <span class="token operator">|</span> wx<span class="token punctuation">.</span>ALIGN_CENTER_VERTICAL<span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span>

        self<span class="token punctuation">.</span>st <span class="token operator">=</span> wx<span class="token punctuation">.</span>StaticText<span class="token punctuation">(</span>self<span class="token punctuation">,</span> label<span class="token operator">=</span><span class="token string">&quot;测试&quot;</span><span class="token punctuation">,</span> pos<span class="token operator">=</span><span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        font <span class="token operator">=</span> self<span class="token punctuation">.</span>st<span class="token punctuation">.</span>GetFont<span class="token punctuation">(</span><span class="token punctuation">)</span>
        font<span class="token punctuation">.</span>PointSize <span class="token operator">+=</span> <span class="token number">5</span>
        font <span class="token operator">=</span> font<span class="token punctuation">.</span>Bold<span class="token punctuation">(</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>st<span class="token punctuation">.</span>SetFont<span class="token punctuation">(</span>font<span class="token punctuation">)</span>
        sizer2<span class="token punctuation">.</span>Add<span class="token punctuation">(</span>self<span class="token punctuation">.</span>st<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> wx<span class="token punctuation">.</span>ALL <span class="token operator">|</span> wx<span class="token punctuation">.</span>ALIGN_CENTER_HORIZONTAL <span class="token operator">|</span> wx<span class="token punctuation">.</span>ALIGN_CENTER_VERTICAL<span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span>

        self<span class="token punctuation">.</span>SetSizer<span class="token punctuation">(</span>sizer2<span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>Layout<span class="token punctuation">(</span><span class="token punctuation">)</span>
        sizer2<span class="token punctuation">.</span>Fit<span class="token punctuation">(</span>self<span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>Centre<span class="token punctuation">(</span>wx<span class="token punctuation">.</span>BOTH<span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>m_button2<span class="token punctuation">.</span>Bind<span class="token punctuation">(</span>wx<span class="token punctuation">.</span>EVT_BUTTON<span class="token punctuation">,</span> self<span class="token punctuation">.</span>on_button<span class="token punctuation">)</span>
        <span class="token comment"># 发布一个叫update的主题，触发update_display函数</span>
        pub<span class="token punctuation">.</span>subscribe<span class="token punctuation">(</span>self<span class="token punctuation">.</span>update_display<span class="token punctuation">,</span> <span class="token string">&quot;update&quot;</span><span class="token punctuation">)</span>

    <span class="token keyword">def</span> <span class="token function">update_display</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> msg<span class="token punctuation">,</span> status<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token triple-quoted-string string">&quot;&quot;&quot;
        从线程接收数据并更新显示
        &quot;&quot;&quot;</span>
        <span class="token keyword">if</span> status <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>
            self<span class="token punctuation">.</span>SetStatusText<span class="token punctuation">(</span><span class="token string">'完成！'</span><span class="token punctuation">)</span>
            <span class="token comment"># 启用按钮</span>
            <span class="token comment"># self.m_button2.Enable(True)</span>
            self<span class="token punctuation">.</span>m_button2<span class="token punctuation">.</span>Enable<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token keyword">elif</span> status <span class="token operator">==</span> <span class="token number">1</span><span class="token punctuation">:</span>
            self<span class="token punctuation">.</span>st<span class="token punctuation">.</span>SetLabel<span class="token punctuation">(</span>msg<span class="token punctuation">)</span>

    <span class="token keyword">def</span> <span class="token function">on_button</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> event<span class="token punctuation">)</span><span class="token punctuation">:</span>
        WorkThread<span class="token punctuation">(</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>SetStatusText<span class="token punctuation">(</span><span class="token string">'开始计算，代码不提供中断线程语句，请等待计算结束！'</span><span class="token punctuation">)</span>
        <span class="token comment"># 禁用按钮</span>
        <span class="token comment"># self.m_button2.Enable(False)</span>
        event<span class="token punctuation">.</span>GetEventObject<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>Disable<span class="token punctuation">(</span><span class="token punctuation">)</span>


<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">&quot;__main__&quot;</span><span class="token punctuation">:</span>
    app <span class="token operator">=</span> wx<span class="token punctuation">.</span>App<span class="token punctuation">(</span><span class="token punctuation">)</span>
    MainForm<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>Show<span class="token punctuation">(</span><span class="token punctuation">)</span>
    app<span class="token punctuation">.</span>MainLoop<span class="token punctuation">(</span><span class="token punctuation">)</span>

</code></pre></div><ul><li>示例二</li></ul> <div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">import</span> time
<span class="token keyword">import</span> wx
<span class="token keyword">from</span> threading <span class="token keyword">import</span> Thread
<span class="token keyword">from</span> pubsub <span class="token keyword">import</span> pub


<span class="token keyword">class</span> <span class="token class-name">MainThread</span><span class="token punctuation">(</span>Thread<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token comment"># 线程实例化时立即启动</span>
        Thread<span class="token punctuation">.</span>__init__<span class="token punctuation">(</span>self<span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>start<span class="token punctuation">(</span><span class="token punctuation">)</span>

    <span class="token keyword">def</span> <span class="token function">run</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token comment"># 线程执行的代码</span>
        <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">101</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">0.03</span><span class="token punctuation">)</span>
            <span class="token comment"># 订阅update主题</span>
            wx<span class="token punctuation">.</span>CallAfter<span class="token punctuation">(</span>pub<span class="token punctuation">.</span>sendMessage<span class="token punctuation">,</span> <span class="token string">&quot;update&quot;</span><span class="token punctuation">,</span> msg<span class="token operator">=</span>i<span class="token punctuation">)</span>
            time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">0.5</span><span class="token punctuation">)</span>


<span class="token keyword">class</span> <span class="token class-name">MainForm</span><span class="token punctuation">(</span>wx<span class="token punctuation">.</span>Frame<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> parent<span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        wx<span class="token punctuation">.</span>Frame<span class="token punctuation">.</span>__init__<span class="token punctuation">(</span>self<span class="token punctuation">,</span> parent<span class="token punctuation">,</span> <span class="token builtin">id</span><span class="token operator">=</span>wx<span class="token punctuation">.</span>ID_ANY<span class="token punctuation">,</span> title<span class="token operator">=</span><span class="token string">&quot;Bajins工具&quot;</span><span class="token punctuation">,</span> pos<span class="token operator">=</span>wx<span class="token punctuation">.</span>DefaultPosition<span class="token punctuation">,</span>
                          style<span class="token operator">=</span>wx<span class="token punctuation">.</span>DEFAULT_FRAME_STYLE <span class="token operator">|</span> wx<span class="token punctuation">.</span>TAB_TRAVERSAL<span class="token punctuation">)</span>
        <span class="token comment"># 允许指定最小和最大窗口大小以及窗口大小增量。</span>
        self<span class="token punctuation">.</span>SetSizeHints<span class="token punctuation">(</span>wx<span class="token punctuation">.</span>Size<span class="token punctuation">(</span><span class="token number">400</span><span class="token punctuation">,</span> <span class="token number">30</span><span class="token punctuation">)</span><span class="token punctuation">,</span> wx<span class="token punctuation">.</span>DefaultSize<span class="token punctuation">,</span> wx<span class="token punctuation">.</span>DefaultSize<span class="token punctuation">)</span>

        self<span class="token punctuation">.</span>CreateStatusBar<span class="token punctuation">(</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>SetStatusText<span class="token punctuation">(</span><span class="token string">&quot;启动完成!&quot;</span><span class="token punctuation">)</span>

        sizer <span class="token operator">=</span> wx<span class="token punctuation">.</span>BoxSizer<span class="token punctuation">(</span>wx<span class="token punctuation">.</span>HORIZONTAL<span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>m_button2 <span class="token operator">=</span> wx<span class="token punctuation">.</span>Button<span class="token punctuation">(</span>self<span class="token punctuation">,</span> wx<span class="token punctuation">.</span>ID_ANY<span class="token punctuation">,</span> <span class="token string">&quot;执行线程&quot;</span><span class="token punctuation">)</span>
        sizer<span class="token punctuation">.</span>Add<span class="token punctuation">(</span>self<span class="token punctuation">.</span>m_button2<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> wx<span class="token punctuation">.</span>ALL <span class="token operator">|</span> wx<span class="token punctuation">.</span>ALIGN_CENTER_HORIZONTAL <span class="token operator">|</span> wx<span class="token punctuation">.</span>ALIGN_CENTER_VERTICAL<span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span>

        self<span class="token punctuation">.</span>m_staticText2 <span class="token operator">=</span> wx<span class="token punctuation">.</span>StaticText<span class="token punctuation">(</span>self<span class="token punctuation">,</span> wx<span class="token punctuation">.</span>ID_ANY<span class="token punctuation">,</span> <span class="token string">&quot;0%&quot;</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>m_staticText2<span class="token punctuation">.</span>Wrap<span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>
        sizer<span class="token punctuation">.</span>Add<span class="token punctuation">(</span>self<span class="token punctuation">.</span>m_staticText2<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> wx<span class="token punctuation">.</span>ALL <span class="token operator">|</span> wx<span class="token punctuation">.</span>SIZE_FORCE <span class="token operator">|</span> wx<span class="token punctuation">.</span>ALIGN_CENTER_VERTICAL<span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span>

        self<span class="token punctuation">.</span>m_gauge1 <span class="token operator">=</span> wx<span class="token punctuation">.</span>Gauge<span class="token punctuation">(</span>self<span class="token punctuation">,</span> wx<span class="token punctuation">.</span>ID_ANY<span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>m_gauge1<span class="token punctuation">.</span>SetValue<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span>
        sizer<span class="token punctuation">.</span>Add<span class="token punctuation">(</span>self<span class="token punctuation">.</span>m_gauge1<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> wx<span class="token punctuation">.</span>ALL <span class="token operator">|</span> wx<span class="token punctuation">.</span>ALIGN_CENTER_VERTICAL<span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span>

        self<span class="token punctuation">.</span>SetSizer<span class="token punctuation">(</span>sizer<span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>Layout<span class="token punctuation">(</span><span class="token punctuation">)</span>
        sizer<span class="token punctuation">.</span>Fit<span class="token punctuation">(</span>self<span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>Centre<span class="token punctuation">(</span>wx<span class="token punctuation">.</span>BOTH<span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>m_button2<span class="token punctuation">.</span>Bind<span class="token punctuation">(</span>wx<span class="token punctuation">.</span>EVT_BUTTON<span class="token punctuation">,</span> self<span class="token punctuation">.</span>on_button<span class="token punctuation">)</span>
        <span class="token comment"># 发布一个叫update的主题，触发update_display函数</span>
        pub<span class="token punctuation">.</span>subscribe<span class="token punctuation">(</span>self<span class="token punctuation">.</span>update_display<span class="token punctuation">,</span> <span class="token string">&quot;update&quot;</span><span class="token punctuation">)</span>

    <span class="token keyword">def</span> <span class="token function">update_display</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> msg<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token comment"># 如果是数字，说明线程正在执行，显示数字</span>
        <span class="token keyword">if</span> <span class="token builtin">isinstance</span><span class="token punctuation">(</span>msg<span class="token punctuation">,</span> <span class="token builtin">int</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            self<span class="token punctuation">.</span>m_staticText2<span class="token punctuation">.</span>SetLabel<span class="token punctuation">(</span><span class="token string">&quot;%s%%&quot;</span> <span class="token operator">%</span> msg<span class="token punctuation">)</span>
            self<span class="token punctuation">.</span>m_gauge1<span class="token punctuation">.</span>SetValue<span class="token punctuation">(</span>msg<span class="token punctuation">)</span>
        <span class="token keyword">else</span><span class="token punctuation">:</span>
            <span class="token comment"># 否则线程未执行，将按钮重新开启</span>
            self<span class="token punctuation">.</span>m_staticText2<span class="token punctuation">.</span>SetLabel<span class="token punctuation">(</span><span class="token string">&quot;%s&quot;</span> <span class="token operator">%</span> msg<span class="token punctuation">)</span>
            self<span class="token punctuation">.</span>m_button2<span class="token punctuation">.</span>Enable<span class="token punctuation">(</span><span class="token punctuation">)</span>

    <span class="token keyword">def</span> <span class="token function">on_button</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> event<span class="token punctuation">)</span><span class="token punctuation">:</span>
        MainThread<span class="token punctuation">(</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>SetStatusText<span class="token punctuation">(</span><span class="token string">&quot;线程开始&quot;</span><span class="token punctuation">)</span>
        <span class="token comment"># 禁用按钮</span>
        event<span class="token punctuation">.</span>GetEventObject<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>Disable<span class="token punctuation">(</span><span class="token punctuation">)</span>


<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">&quot;__main__&quot;</span><span class="token punctuation">:</span>
    app <span class="token operator">=</span> wx<span class="token punctuation">.</span>App<span class="token punctuation">(</span><span class="token punctuation">)</span>
    MainForm<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>Show<span class="token punctuation">(</span><span class="token punctuation">)</span>
    app<span class="token punctuation">.</span>MainLoop<span class="token punctuation">(</span><span class="token punctuation">)</span>

</code></pre></div><h2 id="pyside"><a href="#pyside" class="header-anchor">#</a> PySide</h2> <blockquote><p><code>Pyside</code>最初是为诺基亚开发的，而它是Qt技术的所有者。
当诺基亚将Qt出售给<code>Digia</code>（现在是Qt公司）时，Qt4原始<code>Pyside</code>代码的所有版权也转移到了Qt公司。</p></blockquote> <blockquote><p><code>Qt for Python</code>项目旨在为<code>Qt5</code>提供<code>PySide</code>模块的完整端口。该开发于2015年5月在GitHub上开始。
该项目设法将<code>Pyside</code>移植到<code>Qt5.3</code>,<code>5.4</code>和<code>5.5</code>。</p></blockquote> <blockquote><p>该<code>PySide2</code>模块于2018年6月中旬作为技术预览版发布（支持<code>Qt5.11</code>）。并在同年12月支持<code>Qt5.12</code>并正式发布</p></blockquote> <blockquote><p><code>PySide2</code>模块提供对<code>QtCore</code>，<code>QtGui</code>等各个Qt模块的访问。还附带了<code>Shiboken2</code>、<code>CPython</code>绑定代码生成器，
可用于为<code>C</code>或<code>C++</code>代码生成<code>Python</code>绑定。</p></blockquote> <ul><li><a href="https://wiki.qt.io/Qt_for_Python/zh" target="_blank" rel="noopener noreferrer">https://wiki.qt.io/Qt_for_Python/zh<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://gaianote.github.io/2018/09/15/PyQt5%E5%AD%A6%E4%B9%A0%E4%B9%8Bline-edit" target="_blank" rel="noopener noreferrer">https://gaianote.github.io/2018/09/15/PyQt5%E5%AD%A6%E4%B9%A0%E4%B9%8Bline-edit/<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://zmister.com/archives/docs/easy-pyqt" target="_blank" rel="noopener noreferrer">一看就懂的Python图形界面开发：PyQt5/PySide2<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://zmister.com/archives/category/guidevelop/pyqt5_basic" target="_blank" rel="noopener noreferrer">PyQt5基础教程<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <h3 id="多线程防假死动态刷新界面"><a href="#多线程防假死动态刷新界面" class="header-anchor">#</a> 多线程防假死动态刷新界面</h3> <div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">from</span> PyQt5 <span class="token keyword">import</span> QtWidgets<span class="token punctuation">,</span> QtCore
<span class="token keyword">import</span> sys
<span class="token keyword">from</span> PyQt5<span class="token punctuation">.</span>QtCore <span class="token keyword">import</span> <span class="token operator">*</span>
<span class="token keyword">import</span> time
 
 
<span class="token comment"># 继承QThread</span>
<span class="token keyword">class</span> <span class="token class-name">Runthread</span><span class="token punctuation">(</span>QtCore<span class="token punctuation">.</span>QThread<span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment">#  通过类成员对象定义信号对象</span>
    _signal <span class="token operator">=</span> pyqtSignal<span class="token punctuation">(</span><span class="token builtin">str</span><span class="token punctuation">)</span>
 
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token builtin">super</span><span class="token punctuation">(</span>Runthread<span class="token punctuation">,</span> self<span class="token punctuation">)</span><span class="token punctuation">.</span>__init__<span class="token punctuation">(</span><span class="token punctuation">)</span>
 
    <span class="token keyword">def</span> <span class="token function">__del__</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
        self<span class="token punctuation">.</span>wait<span class="token punctuation">(</span><span class="token punctuation">)</span>
 
    <span class="token keyword">def</span> <span class="token function">run</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
            time<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">0.2</span><span class="token punctuation">)</span>
            <span class="token comment"># 注意这里与_signal = pyqtSignal(str)中的类型相同</span>
            self<span class="token punctuation">.</span>_signal<span class="token punctuation">.</span>emit<span class="token punctuation">(</span><span class="token builtin">str</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">)</span>
 
 
<span class="token keyword">class</span> <span class="token class-name">Example</span><span class="token punctuation">(</span>QtWidgets<span class="token punctuation">.</span>QWidget<span class="token punctuation">)</span><span class="token punctuation">:</span>
 
    <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token builtin">super</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>__init__<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token comment"># 按钮初始化</span>
        self<span class="token punctuation">.</span>button <span class="token operator">=</span> QtWidgets<span class="token punctuation">.</span>QPushButton<span class="token punctuation">(</span><span class="token string">'开始'</span><span class="token punctuation">,</span> self<span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>button<span class="token punctuation">.</span>setToolTip<span class="token punctuation">(</span><span class="token string">'这是一个 &lt;b&gt;QPushButton&lt;/b&gt; widget'</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>button<span class="token punctuation">.</span>resize<span class="token punctuation">(</span>self<span class="token punctuation">.</span>button<span class="token punctuation">.</span>sizeHint<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>button<span class="token punctuation">.</span>move<span class="token punctuation">(</span><span class="token number">120</span><span class="token punctuation">,</span> <span class="token number">80</span><span class="token punctuation">)</span>
        <span class="token comment"># 绑定多线程触发事件</span>
        self<span class="token punctuation">.</span>button<span class="token punctuation">.</span>clicked<span class="token punctuation">.</span>connect<span class="token punctuation">(</span>self<span class="token punctuation">.</span>start_login<span class="token punctuation">)</span>
 
        <span class="token comment"># 进度条设置</span>
        self<span class="token punctuation">.</span>pbar <span class="token operator">=</span> QtWidgets<span class="token punctuation">.</span>QProgressBar<span class="token punctuation">(</span>self<span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>pbar<span class="token punctuation">.</span>setGeometry<span class="token punctuation">(</span><span class="token number">50</span><span class="token punctuation">,</span> <span class="token number">50</span><span class="token punctuation">,</span> <span class="token number">210</span><span class="token punctuation">,</span> <span class="token number">25</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>pbar<span class="token punctuation">.</span>setValue<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span>
 
        <span class="token comment"># 窗口初始化</span>
        self<span class="token punctuation">.</span>setGeometry<span class="token punctuation">(</span><span class="token number">300</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>setWindowTitle<span class="token punctuation">(</span><span class="token string">'OmegaXYZ.com'</span><span class="token punctuation">)</span>
        self<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span>
 
        self<span class="token punctuation">.</span>thread <span class="token operator">=</span> <span class="token boolean">None</span>  <span class="token comment"># 初始化线程</span>
 
    <span class="token keyword">def</span> <span class="token function">start_login</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token comment"># 创建线程</span>
        self<span class="token punctuation">.</span>thread <span class="token operator">=</span> Runthread<span class="token punctuation">(</span><span class="token punctuation">)</span>
        <span class="token comment"># 连接信号,进程连接回传到GUI的事件</span>
        self<span class="token punctuation">.</span>thread<span class="token punctuation">.</span>_signal<span class="token punctuation">.</span>connect<span class="token punctuation">(</span>self<span class="token punctuation">.</span>call_backlog<span class="token punctuation">)</span>
        <span class="token comment"># 开始线程</span>
        self<span class="token punctuation">.</span>thread<span class="token punctuation">.</span>start<span class="token punctuation">(</span><span class="token punctuation">)</span>
 
    <span class="token keyword">def</span> <span class="token function">call_backlog</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> msg<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token comment"># 将线程的参数传入进度条</span>
        self<span class="token punctuation">.</span>pbar<span class="token punctuation">.</span>setValue<span class="token punctuation">(</span><span class="token builtin">int</span><span class="token punctuation">(</span>msg<span class="token punctuation">)</span><span class="token punctuation">)</span>
 
 
<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">&quot;__main__&quot;</span><span class="token punctuation">:</span>
    app <span class="token operator">=</span> QtWidgets<span class="token punctuation">.</span>QApplication<span class="token punctuation">(</span>sys<span class="token punctuation">.</span>argv<span class="token punctuation">)</span>
    myshow <span class="token operator">=</span> Example<span class="token punctuation">(</span><span class="token punctuation">)</span>
    myshow<span class="token punctuation">.</span>show<span class="token punctuation">(</span><span class="token punctuation">)</span>
    sys<span class="token punctuation">.</span>exit<span class="token punctuation">(</span>app<span class="token punctuation">.</span>exec_<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

</code></pre></div><h2 id="打包"><a href="#打包" class="header-anchor">#</a> 打包</h2> <h3 id="pyinstalle"><a href="#pyinstalle" class="header-anchor">#</a> PyInstalle</h3> <blockquote><p>PyInstaller 是一个十分有用的第三方库，可以用来打包<code>Python</code>应用程序，
打包完的程序就可以在没有安装<code>Python</code>解释器的机器上运行了。</p> <p>它能够在<code>Windows</code>、<code>Linux</code>、<code>Mac OS X</code>等操作系统下将<code>Python</code>源文件打包，通过对源文件打包，
<code>Python</code>程序可以在没有安装<code>Python</code>的环境中运行，也可以作为一个独立文件方便传递和管理。</p></blockquote> <blockquote><p><code>PyInstaller</code>支持<code>Python 2.7</code>和<code>Python 3.4 +</code>。可以在<code>Windows</code>、<code>Mac OS X</code>和<code>Linux</code>上使用，
但是并不是跨平台的，而是说你要是希望打包成<code>.exe</code>文件，需要在<code>Windows</code>系统上运行<code>PyInstaller</code>进行打包工作。</p></blockquote> <ul><li><a href="https://github.com/pyinstaller" target="_blank" rel="noopener noreferrer">https://github.com/pyinstaller<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://hoxis.github.io/python-pyinstaller.html" target="_blank" rel="noopener noreferrer">https://hoxis.github.io/python-pyinstaller.html<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <p><strong>安装<code>pyinstalle</code></strong></p> <div class="language-bash extra-class"><pre class="language-bash"><code>pip <span class="token function">install</span> pyinstaller
</code></pre></div><p><strong>生成单文件</strong></p> <div class="language-bash extra-class"><pre class="language-bash"><code>pyinstaller -F app.py
<span class="token comment"># windows打包为运行不显示命令行窗口的程序</span>
pyinstaller -F -w -n<span class="token operator">=</span>BajinsWallpaper app.py
</code></pre></div><p><strong>生成安装目录</strong></p> <blockquote><p>此方式可借用其他第三方封包工具封装为exe安装程序，比如<code>NSIS</code>、<code>InnoSetup</code></p> <p>注意：执行命令前先将目录下<code>build</code>、<code>dist</code>目录删除，并将<code>spec</code>后缀的文件也删除</p></blockquote> <div class="language-bash extra-class"><pre class="language-bash"><code>pyinstaller -D -w -n<span class="token operator">=</span>BajinsWallpaper app.py
</code></pre></div><p><strong>静态文件处理</strong></p> <ul><li>在<code>.spec</code>后缀文件中修改<code>datas=[]</code></li></ul> <blockquote><p>每一个文件用<code>(a,b)</code>进行描述，其中<code>a</code>是源文件，<code>b</code>是目标路径
也就是最终打包<code>exe</code>执行时，它需要把这些静态资源解压出来，那么解压到哪里呢就需要<code>b</code>来指定，它是一个文件夹</p></blockquote> <div class="language-bash extra-class"><pre class="language-bash"><code><span class="token assign-left variable">datas</span><span class="token operator">=</span><span class="token punctuation">[</span><span class="token punctuation">(</span><span class="token string">'view/imges/*'</span>, <span class="token string">'.'</span><span class="token punctuation">)</span>, <span class="token punctuation">(</span><span class="token string">'view/static/logo.png'</span>,<span class="token string">'view/static/'</span><span class="token punctuation">)</span><span class="token punctuation">]</span>,
</code></pre></div><ul><li>使用<code>--add-data</code>命令参数</li></ul> <blockquote><p>其实就是修改<code>.spec</code>后缀文件中的<code>datas=[]</code></p></blockquote> <blockquote><p><code>;</code>前面是本地文件路径，后面是打包中所处的位置</p></blockquote> <div class="language-bash extra-class"><pre class="language-bash"><code>--add-data view<span class="token punctuation">\</span>static<span class="token punctuation">\</span>*<span class="token punctuation">;</span>view<span class="token punctuation">\</span>static --add-data <span class="token string">&quot;list.txt;.&quot;</span>
</code></pre></div><p><strong><code>pyinstalle</code>参数</strong></p> <ul><li><a href="https://pyinstaller.readthedocs.io/en/latest/usage.html#options" target="_blank" rel="noopener noreferrer">https://pyinstaller.readthedocs.io/en/latest/usage.html#options<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <ul><li>通用参数</li></ul> <table><thead><tr><th>参数名</th> <th>描述</th> <th>说明</th></tr></thead> <tbody><tr><td>-h</td> <td>显示帮助</td> <td>无</td></tr> <tr><td>-v</td> <td>显示版本号</td> <td>无</td></tr> <tr><td>–distpath</td> <td>生成文件放在哪里</td> <td>默认：当前目录的dist文件夹内</td></tr> <tr><td>–workpath</td> <td>生成过程中的中间文件放在哪里</td> <td>默认：当前目录的build文件夹内</td></tr> <tr><td>-y</td> <td>如果dist文件夹内已经存在生成文件，则不询问用户，直接覆盖</td> <td>默认：询问是否覆盖</td></tr> <tr><td>–upx-dir UPX_DIR</td> <td>指定upx工具的目录</td> <td>默认：execution path</td></tr> <tr><td>-a</td> <td>不包含unicode支持</td> <td>默认：尽可能支持unicode</td></tr> <tr><td>–clean</td> <td>在本次编译开始时，清空上一次编译生成的各种文件</td> <td>默认：不清除</td></tr> <tr><td>–log-level LEVEL</td> <td>控制编译时pyi打印的信息</td> <td>一共有6个等级，由低到高分别为TRACE DEBUG INFO(默认) WARN ERROR CRITICAL。也就是默认清空下，不打印TRACE和DEBUG信息</td></tr></tbody></table> <ul><li>与生成结果有关的参数</li></ul> <table><thead><tr><th>参数名</th> <th>描述</th> <th>说明</th></tr></thead> <tbody><tr><td>-D</td> <td>生成one-folder的程序（默认）</td> <td>生成结果是一个目录，各种第三方依赖、资源和exe同时存储在该目录</td></tr> <tr><td>-F</td> <td>生成one-file的程序</td> <td>生成结果是一个exe文件，所有的第三方依赖、资源和代码均被打包进该exe内</td></tr> <tr><td>–specpath</td> <td>指定.spec文件的存储路径</td> <td>默认：当前目录</td></tr> <tr><td>-n</td> <td>生成的.exe文件和.spec的文件名</td> <td>默认：用户脚本的名称，即main.py和main.spec</td></tr></tbody></table> <ul><li>指定打包哪些资源、代码</li></ul> <table><thead><tr><th>参数名</th> <th>描述</th> <th>说明</th></tr></thead> <tbody><tr><td>–add-data</td> <td>打包额外资源</td> <td>用法：pyinstaller main.py --add-data=src;dest。windows以;分割，linux以:分割</td></tr> <tr><td>–add-binary</td> <td>打包额外的代码</td> <td>用法：同–add-data。与–add-data不同的是，用binary添加的文件，pyi会分析它引用的文件并把它们一同添加进来</td></tr> <tr><td>-p</td> <td>指定额外的import路径，类似于使用PYTHONPATH</td> <td>参见PYTHONPATH</td></tr> <tr><td>–hidden-import</td> <td>打包额外py库</td> <td>pyi在分析过程中，有些import没有正确分析出来，运行时会报import error，这时可以使用该参数</td></tr> <tr><td>–additional-hooks-dir</td> <td>指定用户的hook目录</td> <td>hook用法参见其他，系统hook在PyInstaller\hooks目录下</td></tr> <tr><td>–runtime-hook</td> <td>指定用户runtime-hook</td> <td>如果设置了此参数，则runtime-hook会在运行main.py之前被运行</td></tr> <tr><td>–exclude-module</td> <td>需要排除的module</td> <td>pyi会分析出很多相互关联的库，但是某些库对用户来说是没用的，可以用这个参数排除这些库，有助于减少生成文件的大小</td></tr> <tr><td>–key</td> <td>pyi会存储字节码，指定加密字节码的key</td> <td>16位的字符串</td></tr></tbody></table> <ul><li>生成参数</li></ul> <table><thead><tr><th>参数名</th> <th>描述</th> <th>说明</th></tr></thead> <tbody><tr><td>-d</td> <td>执行生成的main.exe时，会输出pyi的一些log，有助于查错</td> <td>默认：不输出pyi的log</td></tr> <tr><td>-s</td> <td>优化符号表</td> <td>原文明确表示不建议在windows上使用</td></tr> <tr><td>–noupx</td> <td>强制不使用upx</td> <td>默认：尽可能使用。</td></tr></tbody></table> <ul><li>其他</li></ul> <table><thead><tr><th>参数名</th> <th>描述</th> <th>说明</th></tr></thead> <tbody><tr><td>–runtime-tmpdir</td> <td>指定运行时的临时目录</td> <td>默认：使用系统临时目录</td></tr></tbody></table> <ul><li>Windows和Mac特有的参数</li></ul> <table><thead><tr><th>参数名</th> <th>描述</th> <th>说明</th></tr></thead> <tbody><tr><td>-c</td> <td>显示命令行窗口</td> <td>与-w相反，默认含有此参数</td></tr> <tr><td>-w</td> <td>不显示命令行窗口</td> <td>编写GUI程序时使用此参数有用。</td></tr> <tr><td>-i</td> <td>为main.exe指定图标</td> <td>pyinstaller -i beauty.ico main.py</td></tr></tbody></table> <blockquote><p>使用<code>-i</code>参数设置图标后，由于有缓存，需要移动一下生成的可执行程序的位置，才会显示自定义图标</p></blockquote> <ul><li>Windows特有的参数</li></ul> <table><thead><tr><th>参数名</th> <th>描述</th> <th>说明</th></tr></thead> <tbody><tr><td>–version-file</td> <td>添加版本信息文件</td> <td>pyinstaller --version-file ver.txt</td></tr> <tr><td>-m, --manifest</td> <td>添加manifest文件</td> <td>pyinstaller -m main.manifest</td></tr> <tr><td>-r RESOURCE</td> <td>请参考原文</td> <td></td></tr> <tr><td>–uac-admin</td> <td>请参考原文</td> <td></td></tr> <tr><td>–uac-uiaccess</td> <td>请参考原文</td> <td></td></tr></tbody></table> <p><strong>问题</strong></p> <ul><li>多进程multiprocessing，我们需要创建一个模块</li></ul> <div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">import</span> os
<span class="token keyword">import</span> sys
<span class="token keyword">import</span> multiprocessing
 
<span class="token comment"># Module multiprocessing is organized differently in Python 3.4+</span>
<span class="token keyword">try</span><span class="token punctuation">:</span>
    <span class="token comment"># Python 3.4+</span>
    <span class="token keyword">if</span> sys<span class="token punctuation">.</span>platform<span class="token punctuation">.</span>startswith<span class="token punctuation">(</span><span class="token string">'win'</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">import</span> multiprocessing<span class="token punctuation">.</span>popen_spawn_win32 <span class="token keyword">as</span> forking
    <span class="token keyword">else</span><span class="token punctuation">:</span>
        <span class="token keyword">import</span> multiprocessing<span class="token punctuation">.</span>popen_fork <span class="token keyword">as</span> forking
<span class="token keyword">except</span> ImportError<span class="token punctuation">:</span>
    <span class="token keyword">import</span> multiprocessing<span class="token punctuation">.</span>forking <span class="token keyword">as</span> forking
 
<span class="token keyword">if</span> sys<span class="token punctuation">.</span>platform<span class="token punctuation">.</span>startswith<span class="token punctuation">(</span><span class="token string">'win'</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token comment"># First define a modified version of Popen.</span>
    <span class="token keyword">class</span> <span class="token class-name">_Popen</span><span class="token punctuation">(</span>forking<span class="token punctuation">.</span>Popen<span class="token punctuation">)</span><span class="token punctuation">:</span>
        <span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> <span class="token operator">*</span>args<span class="token punctuation">,</span> <span class="token operator">**</span>kw<span class="token punctuation">)</span><span class="token punctuation">:</span>
            <span class="token keyword">if</span> <span class="token builtin">hasattr</span><span class="token punctuation">(</span>sys<span class="token punctuation">,</span> <span class="token string">'frozen'</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
                <span class="token comment"># We have to set original _MEIPASS2 value from sys._MEIPASS</span>
                <span class="token comment"># to get --onefile mode working.</span>
                os<span class="token punctuation">.</span>putenv<span class="token punctuation">(</span><span class="token string">'_MEIPASS2'</span><span class="token punctuation">,</span> sys<span class="token punctuation">.</span>_MEIPASS<span class="token punctuation">)</span>
            <span class="token keyword">try</span><span class="token punctuation">:</span>
                <span class="token builtin">super</span><span class="token punctuation">(</span>_Popen<span class="token punctuation">,</span> self<span class="token punctuation">)</span><span class="token punctuation">.</span>__init__<span class="token punctuation">(</span><span class="token operator">*</span>args<span class="token punctuation">,</span> <span class="token operator">**</span>kw<span class="token punctuation">)</span>
            <span class="token keyword">finally</span><span class="token punctuation">:</span>
                <span class="token keyword">if</span> <span class="token builtin">hasattr</span><span class="token punctuation">(</span>sys<span class="token punctuation">,</span> <span class="token string">'frozen'</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
                    <span class="token comment"># On some platforms (e.g. AIX) 'os.unsetenv()' is not</span>
                    <span class="token comment"># available. In those cases we cannot delete the variable</span>
                    <span class="token comment"># but only set it to the empty string. The bootloader</span>
                    <span class="token comment"># can handle this case.</span>
                    <span class="token keyword">if</span> <span class="token builtin">hasattr</span><span class="token punctuation">(</span>os<span class="token punctuation">,</span> <span class="token string">'unsetenv'</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
                        os<span class="token punctuation">.</span>unsetenv<span class="token punctuation">(</span><span class="token string">'_MEIPASS2'</span><span class="token punctuation">)</span>
                    <span class="token keyword">else</span><span class="token punctuation">:</span>
                        os<span class="token punctuation">.</span>putenv<span class="token punctuation">(</span><span class="token string">'_MEIPASS2'</span><span class="token punctuation">,</span> <span class="token string">''</span><span class="token punctuation">)</span>
 
    <span class="token comment"># Second override 'Popen' class with our modified version.</span>
    forking<span class="token punctuation">.</span>Popen <span class="token operator">=</span> _Popen
 
</code></pre></div><h3 id="py2exe"><a href="#py2exe" class="header-anchor">#</a> py2exe</h3> <ul><li><a href="http://www.py2exe.org" target="_blank" rel="noopener noreferrer">http://www.py2exe.org/<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://hoxis.github.io/python-py2exe.html" target="_blank" rel="noopener noreferrer">https://hoxis.github.io/python-py2exe.html<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <blockquote><p><code>py2exe</code>是一个将<code>python</code>脚本转换成<code>Windows</code>上的可独立执行的可执行程序（<code>*.exe</code>）的工具，
这样就可以不用装<code>python</code>而在<code>Windows</code>系统上运行这个可执行程序</p></blockquote> <blockquote><p><code>py2exe</code>新版本只支持<code>python3.3</code>以上，可以使用<code>pip install py2exe_py2</code>来安装兼容<code>python2</code>版本；</p> <p>若在<code>python3.6</code>版本下运行报错，请切换到<code>python3.4</code>尝试；</p> <p><code>python3</code>如果是<code>64 位</code>，生成的<code>exe</code>只能在<code>64 位</code>操作系统下运行，使用<code>32 位</code> <code>python</code>可以解决；</p> <p>从<code>Python 3.3</code>，<code>Windows</code>在构建<code>Python</code>时使用的是<code>Visual Studio 2010</code>，因此生成后，
需要手动将<code>msvcr100.dll</code>拷到生成目录下（<code>dist</code>目录），否则最终的文件运行时可能会报错；
或者通过<code>data_files=[(&quot;&quot;,[&quot;MSVCR100.dll&quot;])]</code>, 打包其中；</p></blockquote> <p><strong><code>setup.py</code></strong></p> <div class="language-python extra-class"><pre class="language-python"><code>
<span class="token keyword">from</span> distutils<span class="token punctuation">.</span>core <span class="token keyword">import</span> setup
<span class="token keyword">import</span> py2exe
<span class="token keyword">import</span> sys
 
<span class="token comment"># 允许程序通过双击的形式执行。</span>
sys<span class="token punctuation">.</span>argv<span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token string">'py2exe'</span><span class="token punctuation">)</span>
 
py2exe_options <span class="token operator">=</span> <span class="token punctuation">{</span>
        <span class="token comment"># 需要包含的文件，这里的&quot;sip&quot;是PyQt程序打包时需要添加的，如果不是PyQt程序不需要此项。</span>
        <span class="token string">&quot;includes&quot;</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">&quot;sip&quot;</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
        <span class="token comment"># 需要排除的dll文件，这里的&quot;MSVCP90.dll&quot;文件，</span>
        <span class="token comment"># 如果不排除的话会报error: MSVCP90.dll: No such file or directory错误。</span>
        <span class="token string">&quot;dll_excludes&quot;</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token string">&quot;MSVCP90.dll&quot;</span><span class="token punctuation">,</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
        <span class="token comment"># 为1，则压缩文件</span>
        <span class="token string">&quot;compressed&quot;</span><span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token comment"># 为优化级别，默认为0。</span>
        <span class="token string">&quot;optimize&quot;</span><span class="token punctuation">:</span> <span class="token number">2</span><span class="token punctuation">,</span>
        <span class="token comment"># 不自动包含encodings和codecs。</span>
        <span class="token string">&quot;ascii&quot;</span><span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
        <span class="token triple-quoted-string string">&quot;&quot;&quot;
        将程序打包成单文件（同时还会生成一个zip文件，可设置zipfile = None去除）。
        1：表示pyd和dll文件会被打包到单文件中，且不能从文件系统中加载python模块；
        2：表示pyd和dll文件会被打包到单文件中，但是可以从文件系统中加载python模块。
        64位的Py2exe不要添加本句。
        &quot;&quot;&quot;</span>
        <span class="token string">&quot;bundle_files&quot;</span><span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
        <span class="token punctuation">}</span>
 
setup<span class="token punctuation">(</span>
      name <span class="token operator">=</span> <span class="token string">'PyQt Demo'</span><span class="token punctuation">,</span>
      version <span class="token operator">=</span> <span class="token string">'1.0'</span><span class="token punctuation">,</span>
      <span class="token triple-quoted-string string">&quot;&quot;&quot;
      这里使用的是windows，即没有命令行窗口出现，如果使用console则表示有命令行窗口出现。
      &quot;myico.ico&quot;是程序图标
      &quot;&quot;&quot;</span>
      windows <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">{</span> <span class="token string">&quot;script&quot;</span><span class="token punctuation">:</span><span class="token string">'wordreplace.py'</span><span class="token punctuation">,</span><span class="token string">&quot;icon_resources&quot;</span><span class="token punctuation">:</span><span class="token punctuation">[</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">&quot;myico.ico&quot;</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span> 
      <span class="token triple-quoted-string string">&quot;&quot;&quot;
      把images目录中所有的jpg文件打包到dist/images子目录中。
      把public目录下的test.bat文件打包到dist/static子目录中。
      &quot;&quot;&quot;</span>
      data_files <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">(</span><span class="token string">'images'</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token string">'images\*.jpg'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span><span class="token punctuation">(</span><span class="token string">'static'</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token string">'public\\test.bat'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
      <span class="token comment"># 如果没有此句，会生成一个`library.zip`文件。</span>
      zipfile <span class="token operator">=</span> <span class="token boolean">None</span><span class="token punctuation">,</span>
      options <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token string">'py2exe'</span><span class="token punctuation">:</span> py2exe_options<span class="token punctuation">}</span>
      <span class="token punctuation">)</span>
</code></pre></div><blockquote><p>执行该脚本，会得到一个build文件夹和一个dist文件夹。其中，dist文件夹，就是你得到的打包程序。</p></blockquote> <blockquote><p>如果按照上述代码执行成功，则应该dist文件夹中，只包括程序的exe文件和<code>w9xpopen.exe</code>。<code>w9xpopen.exe</code>是针对<code>windows9x</code>版本的，
一般来说该文件并不需要。</p></blockquote> <blockquote><p>如果<code>bundle_files</code>不为<code>1</code>、<code>2</code>，则<code>dist</code>文件夹中还会包括一些dll文件和pyd文件（<code>Python Dll</code>文件）。
如果<code>bundle_files</code>为<code>2</code>，<code>dist</code>文件夹会包括一个<code>python##.dll</code>文件，如果为1则不会。</p></blockquote></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/woytu/notes-vuepress/edit/master//Python/PythonGUI.md" target="_blank" rel="noopener noreferrer">在 GitHub 上编辑此页</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2021-07-21 14:05:36</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/Python/" class="prev router-link-active">
        Python
      </a></span> <span class="next"><a href="/Python/Python爬虫.html">
        Python爬虫
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="/assets/js/app.269009d6.js" defer></script><script src="/assets/js/3.974b9720.js" defer></script><script src="/assets/js/43.ca8f222e.js" defer></script>
  </body>
</html>
